﻿using System;
using System.Html;
using enyo;

namespace JenkinsView
{
    public class JobDetailModel : Model
    {
        private bool _refresh;
        internal void ReadJobDetailData(bool refresh)
        {
            if (DomainModel.Model.JobManager.SelectedJobIndex < 0)
                return;
            _refresh = refresh;

            var job = DomainModel.Model.JobManager.ListCurrentJob();
            string url = job.Url + DomainModel.API_SUFFIX;
            Console.Log("Call url: " + url);
            var ajax = new enyo.Ajax(new AjaxProperties()
            {
                Url = url
            });

            ajax.Go(new
            {
            });

            ajax.Response(this, this.GotJobData);
            ajax.Error(this, this.GotJobError);

            DomainModel.Model.NotifyAll(DomainModel.ASYNC_CALL, DomainModel.WAIT);
        }

        public BuildData[] ListJobDetails()
        {
            return _jobData.Builds;
        }

        private JenkinsJobData _jobData;
        private object GotJobData(Async call, object data)
        {
            //Console.Log(data);

            _jobData = data as JenkinsJobData;

            DomainModel.Model.NotifyAll(DomainModel.ASYNC_CALL, DomainModel.DONE);            
            if (!_refresh)
                DomainModel.Model.NotifyAll(DomainModel.PANEL_NAVIGATE, DomainModel.NEXT_VIEW);
            Notify(DomainModel.JOB_DETAIL_LIST_CHANGE, null);
            return null;
        }

        private object GotJobError(Async call, object data)
        {
            Console.Log("Ajax call error.");
            DomainModel.Model.NotifyAll(DomainModel.ASYNC_CALL, DomainModel.DONE);
            DomainModel.Model.NotifyAll(DomainModel.ERROR, null);
            return null;
        }

        public void ToggleJobBuildableStatus(bool buildable)
        {
            var job = DomainModel.Model.JobManager.ListCurrentJob();
            var url = job.Url + (buildable ? "enable" : "disable");
            var ajax = new enyo.Ajax(new AjaxProperties()
            {
                Url = url,
                Method = "POST",
                HandleAs = enyo.HandleAs.Text
            });

            DomainModel.Model.ApplyBasicAuthentication(ajax);

            ajax.Go(new
            {
            });

            ajax.Response(this, this.ToggleJobSuccess);
            ajax.Error(this, this.ToggleJobFailure);

            DomainModel.Model.NotifyAll(DomainModel.ASYNC_CALL, DomainModel.WAIT);
        }

        private object ToggleJobSuccess(Async call, object data)
        {
            DomainModel.Model.NotifyAll(DomainModel.ASYNC_CALL, DomainModel.DONE);
            var job = DomainModel.Model.JobManager.ListCurrentJob();
            job.Buildable = !job.Buildable;
            this.Notify(DomainModel.JOB_BUILDABLE_CHANGE, null);
            return null;
        }

        private object ToggleJobFailure(Async call, object data)
        {
            DomainModel.Model.NotifyAll(DomainModel.ASYNC_CALL, DomainModel.DONE);
            DomainModel.Model.NotifyAll(DomainModel.ERROR, null);
            Console.Log("notify not change???");
            this.Notify(DomainModel.JOB_BUILDABLE_CHANGE, null);
            return null;
        }

        public void TriggerBuild()
        {
            var job = DomainModel.Model.JobManager.ListCurrentJob();
            var url = job.Url + "build";
            var ajax = new enyo.Ajax(new AjaxProperties()
            {
                Url = url,
                Method = "POST",
                HandleAs = enyo.HandleAs.Text
            });

            DomainModel.Model.ApplyBasicAuthentication(ajax);

            ajax.Go(new
            {
            });

            ajax.Response(this, this.BuildSuccess);
            ajax.Error(this, this.BuildFailure);

            DomainModel.Model.NotifyAll(DomainModel.ASYNC_CALL, DomainModel.WAIT);
        }

        private object BuildSuccess(Async call, object data)
        {
            DomainModel.Model.NotifyAll(DomainModel.ASYNC_CALL, DomainModel.DONE);
            this.Notify(DomainModel.JOB_BUILD_LAUNCH, null);
            return null;
        }

        private object BuildFailure(Async call, object data)
        {
            DomainModel.Model.NotifyAll(DomainModel.ASYNC_CALL, DomainModel.DONE);
            DomainModel.Model.NotifyAll(DomainModel.ERROR, null);
            return null;
        }
    }
}
